package com.kele.service.impl;

import com.kele.mapper.DoctorMapper;
import com.kele.mapper.SectionDoctorMapper;
import com.kele.mapper.SectionMapper;
import com.kele.model.entity.Doctor;
import com.kele.model.entity.Section;
import com.kele.model.entity.SectionDoctor;
import com.kele.model.his.HisDoctor;
import com.kele.service.DoctorService;
import com.sun.scenario.effect.impl.prism.PrImage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.print.Doc;
import java.util.*;
import java.util.function.Function;
import java.util.stream.Collectors;

/**
 * @Description:
 * @Author: Kele
 * @CreateDate: 2022/8/4 18:04
 * @Version:
 */
@Service
public class DoctorServiceImpl implements DoctorService {

    @Autowired
    private SectionMapper sectionMapper;
    @Autowired
    private DoctorMapper doctorMapper;
    @Autowired
    private SectionDoctorMapper sectionDoctorMapper;

    @Override
    public void addDoctorInfo(List<HisDoctor> doctors) {
        List<Doctor> insertInfo = new ArrayList<>();
        List<Doctor> updateInfo = new ArrayList<>();
        List<Integer> updateDoctorIds = new ArrayList<>();
        // 库中的科室
        List<Section> allSection = sectionMapper.getAllSection();
        Map<String, Section> sectionMap = allSection.stream().collect(Collectors.toMap(Section::getHisId, Function.identity()));
        // 库中的医生
        List<Doctor> allDoctor = doctorMapper.getAllDoctor();
        Map<String, Doctor> doctorMap = allDoctor.stream().collect(Collectors.toMap(Doctor::getHisId, Function.identity()));

        /**
         * 1. 先判断我们库中是否存在该医生
         *      存在：替换医生姓名，添加到更新医生list
         *      不存在：再判断添加list中是否已经添加过该医生的信息
         *          添加过：添加医生的科室信息
         *          没添加过：添加
         */
        for (HisDoctor hisDoctor : doctors) {

            // 该医生已存在我们库中
            if (doctorMap.containsKey(hisDoctor.getHisId())) {
                // 如果更新列表中已经存在了该医生的信息
                if (updateInfo.stream().anyMatch(d -> d.getHisId().equals(hisDoctor.getHisId()))) {
                    Optional<Doctor> first = updateInfo.stream().filter(d -> d.getHisId().equals(hisDoctor.getHisId())).findFirst();
                    // 将这次的科室信息添加到原来的对象中
                    first.ifPresent(doctor -> doctor.getSections().add(sectionMap.get(hisDoctor.getHisSectionId())));
                } else { // 更新列表中不存在该医生
                    Doctor doctor = doctorMap.get(hisDoctor.getHisId());
                    doctor.setName(hisDoctor.getName());
                    ArrayList<Section> sections = new ArrayList<>();
                    sections.add(sectionMap.get(hisDoctor.getHisSectionId()));
                    doctor.setSections(sections);
                    updateInfo.add(doctor);
                    updateDoctorIds.add(doctor.getId());
                }

            } else { // 我们库中未存在该医生
                // 若添加列表中已经存在该医生
                if (insertInfo.stream().anyMatch(d -> d.getHisId().equals(hisDoctor.getHisId()))) {
                    Optional<Doctor> first = insertInfo.stream().filter(d -> d.getHisId().equals(hisDoctor.getHisId())).findFirst();
                    first.ifPresent(d -> d.getSections().add(sectionMap.get(hisDoctor.getHisSectionId())));
                }else {
                    // 我们库中和添加列表中都没有该医生的信息
                    Doctor doctor = new Doctor();
                    doctor.setHisId(hisDoctor.getHisId());
                    doctor.setName(hisDoctor.getName());
                    ArrayList<Section> sections = new ArrayList<>();
                    sections.add(sectionMap.get(hisDoctor.getHisSectionId()));
                    doctor.setSections(sections);
                    insertInfo.add(doctor);
                }
            }
        }

        // 添加医生信息
        if (insertInfo.size() > 0){
            doctorMapper.batchInsert(insertInfo);
        }
        // 拼接医生对应的科室信息
        ArrayList<SectionDoctor> sectionDoctors = new ArrayList<>();
        for (Doctor doctor : insertInfo) {
            for (Section section : doctor.getSections()) {
                SectionDoctor sectionDoctor = new SectionDoctor();
                sectionDoctor.setDoctorId(doctor.getId());
                sectionDoctor.setSectionId(section.getId());
                sectionDoctors.add(sectionDoctor);
            }
        }
        // 添加医生所对应的科室的信息
        if (sectionDoctors.size() > 0){
            sectionDoctorMapper.batchInsert(sectionDoctors);
        }

        // 更新医生信息
        if (updateInfo.size() > 0){
            doctorMapper.batchUpdate(updateInfo);
        }
        // 删除更新的医生所对应的科室信息
        if (updateDoctorIds.size() > 0){
            sectionDoctorMapper.deleteRelByDoctorId(updateDoctorIds);
        }
        // 拼接更新的医生所对应的科室信息
        ArrayList<SectionDoctor> sectionDoctorsRel = new ArrayList<>();
        for (Doctor doctor : updateInfo) {
            for (Section section : doctor.getSections()) {
                SectionDoctor sectionDoctor = new SectionDoctor();
                sectionDoctor.setDoctorId(doctor.getId());
                sectionDoctor.setSectionId(section.getId());
                sectionDoctorsRel.add(sectionDoctor);
            }
        }
        // 添加更新的医生所对应的科室信息
        if (sectionDoctorsRel.size() > 0) {
            sectionDoctorMapper.batchInsert(sectionDoctorsRel);
        }

    }
}
